#include "Engine\Engine.h"
#include "Process\Process.h"
#include "Core\Core.h"
#include "XML\XMLTreeNode.h"
#include "GraphicsManager.h"
#include "Utils\LanguageManager\LanguageManager.h"
#include "Font\FontManager.h"
#include "InputManager.h"
#include "Camera\Camera.h"

CEngine::~CEngine(void)
{
	DeInit();
}

void CEngine::Init(HWND hWnd)
{
	m_Logger = new CLogger();
	m_core = new CCore();
	m_core->Init(hWnd, m_SEngineInitParams);

	m_process->Init();
}
void CEngine::DeInit()
{
	CHECKED_DELETE(m_core);
	CHECKED_DELETE(m_Logger);
	CHECKED_DELETE(m_process);
}
void CEngine::Update()
{
	m_Timer.Update();
	m_process->Update(m_Timer.GetElapsedTime());
	m_core->Update(m_Timer.GetElapsedTime());
	if(CORE->GetInputManager()->IsUpDown(IDV_KEYBOARD,KEY_F1))
	{
		m_process->ChangeRenderDebugInfo();
	}
}

void CEngine::UpdateInputAction()
{

}
void CEngine::Render()
{
	CGraphicsManager *l_GraphicManager = m_core->GetGraphicsManager();
	l_GraphicManager->BeginRendering();//setear los estados de pinatado
	//TODO ENRIC
	//CCamera *camera = m_process->GetCamera();
	//l_GraphicManager->SetupMatrices(camera);
	l_GraphicManager->SetupMatrices(m_process->GetCamera());//Configurar las camaras de visualizacion

	RenderScene(l_GraphicManager);

	l_GraphicManager->EndRendering();
}

void CEngine::RenderScene(CGraphicsManager *l_GraphicsManager)
{
	//Pintado 3d:
	m_core->Render(m_Timer.GetElapsedTime());
	m_process->RenderScene(l_GraphicsManager,m_Timer.GetElapsedTime());

	//Pintado 2d:
	l_GraphicsManager->DisableZBuffering();
	l_GraphicsManager->EnableAlphaBlend();
	
	m_process->RenderScene2D(l_GraphicsManager,m_core->GetFontManager(),m_Timer.GetElapsedTime());
	if(m_bDebugInfo)
	{
		uint32 y=m_process->RenderDebugInfo(l_GraphicsManager, CORE->GetFontManager(),m_Timer.GetFPS());
		l_GraphicsManager->DrawRectangle2D(Vect2i(0,0),100,y,colRED, 3,3, colBLACK);
		m_process->RenderDebugInfo(l_GraphicsManager, CORE->GetFontManager(),m_Timer.GetFPS());
	}
	else
	{
		m_process->RenderDebugInfo(l_GraphicsManager, CORE->GetFontManager(),m_Timer.GetFPS());
	}

	//m_core->GetGUIManager()->Render();

	l_GraphicsManager->DisableAlphaBlend();
	l_GraphicsManager->EnableZBuffering();
}
void CEngine::SetProcess(CProcess *l_process)
{
	m_process=l_process;
}

void CEngine::LoadXml(const std::string& pathInitXml)
{
	CXMLTreeNode newFile;
	if (!newFile.LoadFile(pathInitXml.c_str()))
	{
		printf("ERROR loading the file.");
	}
	CXMLTreeNode  mParent = newFile["Config"];
	if(mParent.Exists())
	{
		int count1 = mParent.GetNumChildren();
		for (int j=0; j<count1; ++j)
		{
			CXMLTreeNode  m = newFile["windowsettings"];
			if (m.Exists())
			{
				int count = m.GetNumChildren();
				for (int i = 0; i < count; ++i)
				{
					m_SEngineInitParams.m_ScreenResolution=m(i).GetVect2iProperty("ScreenResolution", (800,600));
					m_SEngineInitParams.m_WindowsPosition=m(i).GetVect2iProperty("WindowsPosition", (800,600));
					m_SEngineInitParams.m_bFullScreen=m(i).GetBoolProperty("FullScreen", false);
				}
			}
			m = newFile["Languages"];
			if (m.Exists())
			{
				m_SEngineInitParams.m_Default=m.GetPszProperty("default");
				int count = m.GetNumChildren();
				for (int i = 0; i < count; ++i)
				{
					m_SEngineInitParams.m_PathLanguage.push_back(m(i).GetPszProperty("languageXML"));
				}
			}
			m = newFile["Fonts"];
			if (m.Exists())
			{
				m_SEngineInitParams.m_PathFont=m.GetPszProperty("fontsXML");
			}
			m = newFile["Mouse"];
			if (m.Exists())
			{
				m_SEngineInitParams.m_bExclusiveModeinMouse=m.GetBoolProperty("exclusiveModeinMouse");
				m_SEngineInitParams.m_bDrawPointerMouse=m.GetBoolProperty("drawPointerMouse");
			}
		}
	}
}